<?php
/**[用户表模型]
 * @Author: 976123967@qq.com
 * @Date:   2014-08-16 15:06:06
 * @Last Modified by:   chen
 * @Last Modified time: 2014-10-14 23:23:02
 */
class UserModel extends  Model{
	protected $tableName = 'user';



	// 自动验证
    // array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
    protected $_validate=array(
    	
        // 用户名验证
        array('username','require','用户名必须填写',1),
     	array('username','check_username','用户名已经存在',1,'callback',1),
     	array('nickname','require','昵称必须填写',1,'regex',3),
     	array('email','require','邮箱必须填写',1,'regex',3),
     	array('email','email','邮箱格式不对',1,'regex',3),

     	array('password','require','密码必须填写',1,'regex',3),
     	array('password','/^.{6,}$/','密码长度至少6位',1,'regex',3),
     	array('passwords','require','确认密码必须填写',1,'regex',3),
     	array('passwords','password','确认密码不正确',1,'confirm',3), 





    );
    /**
	 * [check_username 验证用户名称是否重复]
	 * @param  [type] $con [description]
	 * @return [type]      [description]
	 */
	public function check_username($con)
	{
		if($this->where(array('username'=>$con))->getField('uid'))
		{
			return false;
		}
		else
			return true;
	}


	// 自动完成
    protected $_auto = array (

        // 时间
        array('reg_time','time',1,'function'), 
        array('login_time','time',1,'function'), 
        //ip
        array('login_ip','_ip',1,'callback'),
        // 密码
        array('password','md5',3,'function'),
    );



    // ip
    public function _ip()
    {
    	return get_client_ip();
    }


	/**
	 * [find_one 获取信息]
	 * @param  [type] $uid [description]
	 * @return [type]      [description]
	 */
	public function find_one($uid)
	{
		$data = $this->find($uid);
		// p($data);die;
		$data['login_time'] = date('Y-m-d',$data['login_time']);
		$data['login_ip'] = $data['login_ip'];
		return $data;
	}


	/**
	 * [update_info 更新个人资料]
	 * @return [type] [description]
	 */
	public function update_info()
	{
		$nickname = I('post.nickname');
		if(!$nickname)
		{
			$this->error='请输入昵称';
			return false;
		}
		$email = I('post.email');
		$preg = "/^([a-zA-Z0-9_\-\.])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/i";
		if(!$email)
		{
			$this->error='请输入邮箱';
			return false;
		}
		elseif(!preg_match($preg, $email))
		{
			$this->error='邮箱格式不对';
			return false;
		}
		$data = array(
			'nickname'=>$nickname,
			'email'=>$email,
			'uid'=>session('uid')
		);
		$this->save($data);
		return true;

	}




	/**
	 * [update_password 修改密码]
	 * @return [type] [description]
	 */
	public function update_password()
	{
		// 新密码验证
		$password = I('post.password');
		if(!$password)
		{
			$this->error='请输入新密码';
			return false;
		}

		if(strlen($password)<6)
		{
			$this->error='新密码长度至少6位';
			return false;
		}
		// 确认密码验证
		$passwords = I('post.passwords');
		if(!$passwords)
		{
			$this->error='请输入确认新密码';
			return false;
		}
		// 旧密码
		$oldpassword = I('post.oldpassword');
		if(!$oldpassword)
		{
			$this->error='请输入旧密码';
			return false;
		}
		$user= $this->find_one(session('uid'));

		if($user['password']!=MD5($oldpassword))
		{
			$this->error='旧密码错误';
			return false;
		}
		// 更新
		$this->where(array('uid'=>session('uid')))->save(array('password'=>md5($password)));
		return true;
	}


	/**
	 * [get_admin 管理员列表]
	 * @return [type] [description]
	 */
	public function get_admin()
	{
		$where['role']=1;
		$order='uid desc';
		// 总数
		$count = $this->where($where)->count();
		import('ORG.Util.Page');// 导入分页类
		$page = new Page($count,20);
		$data = $this->where($where)->order($order)->limit($page->firstRow.','.$page->listRows)->select();
		if(!$data) return $data;

		foreach($data as $k=>$v)
		{
			$data[$k]['lock'] = $v['lock'] ?'锁定':'正常';
			$data[$k]['login_time'] = date('Y-m-d',$v['login_time']);
		}
		$data['page']=$page->show();
		return $data;

	}

	/**
	 * [get_user 会员列表]
	 * @return [type] [description]
	 */
	public function get_user()
	{
		// 不是管理员的会员
		$where['role']=array('neq',1);

		// 注册时间
		// 开始时间
		$starttime = I('get.search_begin_time');
		if($starttime)
		{
			$where[] ='reg_time >='.strtotime($starttime) ;
		}
		// 结束时间
		$endtime = I('get.search_end_time');
		if($endtime)
		{
			//需要修正时间应该是到这一天的结束，所以要加上一天的时间
			$where[] ='reg_time <='.(strtotime($endtime)+24*3600) ;
		}

		// 关键字查找
		$keyword = I('get.search_keyword');
		if($keyword)
		{
			$where['username'] = array('like','%'.$keyword.'%');
		}



		$order='uid desc';
		// 总数
		$count = $this->where($where)->count();
		import('ORG.Util.Page');// 导入分页类
		$page = new Page($count,20);
		$data = $this->where($where)->order($order)->limit($page->firstRow.','.$page->listRows)->select();
		//echo $this->_sql();die;
		if(!$data) return $data;

		foreach($data as $k=>$v)
		{
			$data[$k]['lock'] = $v['lock'] ?'锁定':'正常';
			$data[$k]['login_time'] = date('Y-m-d',$v['login_time']);
			$data[$k]['reg_time'] = date('Y-m-d',$v['reg_time']);
		}
		$data['page']=$page->show();
		return $data;

	}


	/**
	 * [add_user 执行添加]
	 */
	public function add_user()
	{
		$this->add();
	}


	/**
	 * [del_user 删除用户]
	 * @param  [type] $uid [description]
	 * @return [type]      [description]
	 */
	public function del_user($uid)
	{
		if($uid ==1)
		{
			$this->error='不能删除该账户';
			return false;
		}
		if($uid==$_SESSION['uid'])
		{
			$this->error='不能删除自己';
			return false;
		}
		$this->delete($uid);
		return true;
	}

	/**
	 * [save_user 保存用户]
	 * @param  [type] $uid [description]
	 * @return [type]      [description]
	 */
	public function save_user()
	{
		$this->save();
	}


	/**
	 * [update_lock 锁定管理员]
	 * @param  [type] $lock [description]
	 * @return [type]       [description]
	 */
	public function update_lock($lock)
	{
		$uid = I('post.uid');
		if(empty($uid))
		{
			$this->error='没有管理员用于操作';
			return false;
		}


		// 循环更改
		foreach($uid as $k=>$v)
		{
			$data =array(
				'lock'=>$lock
			);
			$this->where(array('uid'=>$v))->save($data);

		}
		return true;
	}


	/**
	 * [delete_check 删除]
	 * @return [type] [description]
	 */
	public function delete_check()
	{
		// 验证
		$uid = I('post.uid');
		if(empty($uid))
		{
			$this->error='没有管理员用于删除';
			return false;
		}
		// 删除
		foreach($uid as $k=>$v)
		{
			$this->del_user($v);
		}
		return true;
	}


	/**
	 * [check_code 比较验证码]
	 * @return [type] [description]
	 */
	public function check_code()
	{
		$code = I('post.code',null,'strtoupper');
		if($_SESSION['code'] != $code) 
		{
   			$this->error="验证码错误！";
   			return false;
		}
		return true;
	}

	/**
	 * [validate_login 登录]
	 * @return [type] [description]
	 */
	public function validate_login()
	{
		// 用户验证
		$username = I('post.username');
		if(!$username)
		{
			$this->error="请输入用户名";
   			return false;
		}
		// 密码验证
		$password = I('post.password');
		if(!$password)
		{
			$this->error="请输入密码";
   			return false;
		}
		// 验证是否正确
		if(!$user = $this->where(array('username'=>$username))->find())
		{
			$this->error="用户名或密码错误";
   			return false;
		}

		if($user['password']!=md5($password))
		{
			$this->error="用户名或密码错误";
   			return false;
		}
		// p($user);die;
		if($user['lock'])
		{
			$this->error="用户已锁定,请联管理员";
   			return false;
		}
		return $user;
	}

	/**
	 * [update_login 更新登录信息]
	 * @return [type] [description]
	 */
	public function update_login()
	{
		$data = array(
			'login_time' => time(),
			'login_ip' => get_client_ip()
		);
		$username = I('post.username');
		// 更新信息
		$this->where(array('username'=>$username))->save($data);
		// 登录次数
		$this->where(array('username'=>$username))->setInc('times',1);
	}

	/**
	 * [check_lock 判断是不是锁定]
	 * @return [type] [description]
	 */
	public function check_lock()
	{
		return $this->where(array('uid'=>session('uid')))->getField('`lock`');
	}

	/**
	 * [update_user 更新会员信息]
	 * @return [type] [description]
	 */
	public function update_user()
	{


		return true;
	}

}